package com.acp.service.impl;

import com.acp.context.Config;
import com.acp.dao.acpo.DcsTaskPerformanceMapper;
import com.acp.dao.acpo.DcsGroupMapper;
import com.acp.model.DcsTaskPerformance;
import com.acp.model.DcsGroup;
import com.acp.param.DcsGroupParam;
import com.acp.result.PageResult;
import com.acp.result.ResultInfo;
import com.acp.result.ResultUtil;
import com.acp.service.DcsGroupService;
import com.acp.util.UUIDBuild;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.controller.construction
 * @Description: 小组管理实现类
 * @author: 张星亮
 * @date: 2019/4/5
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
public class DcsGroupServiceImpl implements DcsGroupService {

    @Resource
    private DcsGroupMapper dcsGroupMapper;

    @Resource
    private DcsTaskPerformanceMapper dcsTaskPerformanceMapper;

    /**
     * 新增小组管理
     * @param dcsGroupParam
     * @return 返回ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     * @throws Exception
     */
    @Override
    public ResultInfo addDcsGroup(DcsGroupParam dcsGroupParam) throws Exception {
        DcsGroup dcsGroup = DcsGroup.builder().id(UUIDBuild.getUUID()).name(dcsGroupParam.getName()).contact_number(dcsGroupParam.getContact_number()).found_time(dcsGroupParam.getFound_time()).protector(dcsGroupParam.getProtector()).group_code(dcsGroupParam.getGroup_code()).build();
        int i = dcsGroupMapper.insert(dcsGroup);
        if (i > 0) {
            return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
        }
        return ResultUtil.createWarning(Config.MESSAGE, 911, null);
    }

    /**
     * 分页查询所有小组管理信息
     * @param name
     * @return 返回PageResult 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @Override
    public PageResult queryDcsGroupList(String name) {
        // 分页插件
        List<DcsGroup> dcsGroupList = dcsGroupMapper.queryDcsGroupList(name);
        if (dcsGroupList.size() > 0) {
            for (int a = 0; a < dcsGroupList.size(); a++) {
                DcsGroup group = dcsGroupList.get(a);
                List<DcsTaskPerformance> list = dcsTaskPerformanceMapper.selectAll(group.getGroup_code());
                String gScore = "0";
                if (list.size() > 0) {
                    int score = 0;
                    for (DcsTaskPerformance dtp : list) {
                        score = score + Integer.valueOf(dtp.getScore());
                    }
                    double groupScore = (double) score / list.size();
                    gScore = String.format("%.2f", groupScore);
                }
                dcsGroupList.get(a).setGroup_score(gScore);
            }
        }
        PageInfo<DcsGroup> pageInfo = new PageInfo<DcsGroup>(dcsGroupList);
        Long count = pageInfo.getTotal();
        return PageResult.success(dcsGroupList, count);
    }

    /**
     * 根据主键删除小组管理
     * @param list
     * @return 返回ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @Override
    public ResultInfo deleteDcsGroupById(List<DcsGroupParam> list) {
        if (list != null && list.size() != 0) {
            for (DcsGroupParam dcsGroupParam : list) {
                String id = dcsGroupParam.getId();
                int i = dcsGroupMapper.deleteByPrimaryKey(id);
            }
            return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
        }
        return ResultUtil.createWarning(Config.MESSAGE, 911, null);
    }

    /**
     * 编辑小组管理
     * @param dcsGroupParam
     * @return 返回ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     * @throws Exception
     */
    @Override
    public ResultInfo editDcsGroup(DcsGroupParam dcsGroupParam) throws Exception {
        DcsGroup dcsGroup = DcsGroup.builder().id(dcsGroupParam.getId()).name(dcsGroupParam.getName()).contact_number(dcsGroupParam.getContact_number()).found_time(dcsGroupParam.getFound_time()).protector(dcsGroupParam.getProtector()).group_code(dcsGroupParam.getGroup_code()).build();
        int i = dcsGroupMapper.updateByPrimaryKey(dcsGroup);
        if (i > 0) {
            return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
        }
        return ResultUtil.createWarning(Config.MESSAGE, 911, null);
    }
}
